package com.ultrawise.survey.module.surevybase.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.ultrawise.survey.common.utils.DateUtils;
import com.ultrawise.survey.common.utils.RequestGetIp;
import com.ultrawise.survey.contants.SurveyConstants;
import com.ultrawise.survey.core.Page;
import com.ultrawise.survey.core.base.BaseContoller;
import com.ultrawise.survey.core.base.ServiceException;
import com.ultrawise.survey.module.surevybase.service.TSurveyInfoBaseService;
import com.ultrawise.survey.module.surevybase.service.TSurveyInfoObjtableService;
import com.ultrawise.survey.module.surevybase.vo.TSurveyInfoBase;
import com.ultrawise.survey.module.surevybase.vo.TSurveyLogPublish;
import com.ultrawise.survey.module.user.vo.TSurveyInfoUsers;

@Controller
@RequestMapping(value = "/symanage/baseinfo")
public class TSurveyInfoBaseControl extends BaseContoller {
	@Autowired
	TSurveyInfoBaseService tSurveyInfoBaseService;
	@Autowired
	TSurveyInfoObjtableService tsurveyinfoobjtableservice;

	@RequestMapping(value = "beforeadd.htm")
	public ModelAndView beforeadd(ModelMap map) {
		map.put("template", SurveyConstants.SURVEY_TEMPLATE);
		return new ModelAndView("/baseinfo/beforeadd");
	}

	/****
	 * 添加问卷信息
	 * 
	 * @param tbase
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "addbase.htm")
	public ModelAndView addbase(TSurveyInfoBase tbase,
			HttpServletRequest request, HttpSession session) {

		try {
			Integer tid = (Integer) tSurveyInfoBaseService.getMaxID(tbase);
			tid = (tid == null) ? 1 : tid + 1;
			tbase.setId(tid);
			tbase.setSy_ispub(SurveyConstants.SURVEY_ISPUB_FALSE);
			tbase.setSy_status(SurveyConstants.SURVEY_BASE_STATUS_NOTSTAR);

			// 增加用户信息
			TSurveyInfoUsers user = (TSurveyInfoUsers) session.getAttribute(SurveyConstants.LOGINED_USER);
			tbase.setPerson(user.getUs_login());// 获取登录账号
			tbase.setCreatetime(DateUtils.toStringAll(new Date()));// 当前时间
			tSurveyInfoBaseService.saveObj(tbase);
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("message", "系统错误，请和管理员联系。");
		}
		request.setAttribute("message", "您已经成功添加问卷信息。");
		request.setAttribute("url", request.getContextPath()
				+ "/symanage/baseinfo/list.htm");
		return new ModelAndView("/massage");
	}

	@RequestMapping(value = "list.htm")
	public ModelAndView listbase(TSurveyInfoBase tbase,
			HttpServletRequest request) {
		Page<TSurveyInfoBase> page = new Page<TSurveyInfoBase>();
		try {
			page = tSurveyInfoBaseService.getlistpage(1, 1000, tbase);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		List<TSurveyInfoBase> lists = page.getPageItems();
		request.setAttribute("message", "您已经成功添加问卷信息。");
		return new ModelAndView("/baseinfo/list", "list", lists);
	}

	/****
	 * 查看文件详细信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "findbase.htm", method = RequestMethod.GET)
	public ModelAndView findbase(HttpServletRequest request,
			TSurveyInfoBase baseinfo, ModelMap map) {

		TSurveyInfoBase tbase = new TSurveyInfoBase();
		try {
			// 编辑前查询对应的id的问卷信息表
			tbase = (TSurveyInfoBase) tSurveyInfoBaseService.queryByID(
					baseinfo.getId(), new TSurveyInfoBase(),
					TSurveyInfoBase.class);
		} catch (ServiceException e) {
			e.printStackTrace();
		}
		map.put("template", SurveyConstants.SURVEY_TEMPLATE);
		return new ModelAndView("/baseinfo/findbaseinfo", "tbase", tbase);
	}

	/****
	 * 更新问卷信息
	 * 
	 * @param tbase
	 * @param request
	 * @param map
	 * @return
	 */
	@RequestMapping(value = "updatebase.htm")
	public ModelAndView updatebase(TSurveyInfoBase tbase,
			HttpServletRequest request, ModelMap map) {

		//System.out.println("update.htm:" + map);
		try {
			if (tbase == null || tbase.getId() == null) {
				tbase = new TSurveyInfoBase();
			}
			// 不更新的内容需要赋值为null
			tbase.setCreatetime(null);
			tbase.setPerson(null);
			tSurveyInfoBaseService.updateObj(tbase);
		} catch (ServiceException e) {
			e.printStackTrace();
			map.put("message", "更新失败，请重试或者联系管理员。");
		}

		map.put("message", "更新成功。");
		map.put("url", request.getContextPath() + "/symanage/baseinfo/list.htm");
		return new ModelAndView("/massage");
	}

	/*****
	 * 暂停问卷信息
	 * 
	 * @param tbase
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "pausebase.htm")
	public ModelAndView pausebase(TSurveyInfoBase tbase,
			HttpServletRequest request) {
		try {
			if (tbase != null && tbase.getId() != null) {
				tbase = (TSurveyInfoBase) tSurveyInfoBaseService.queryByID(
						tbase.getId(), tbase, TSurveyInfoBase.class);
				tbase.setSy_status(SurveyConstants.SURVEY_BASE_STATUS_STOP);// 修改状态为暂停状态
				tSurveyInfoBaseService.updateObj(tbase);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("message", "更新失败，请重试或者联系管理员。");
		}
		request.setAttribute("message", "状态已经修改。");
		request.setAttribute("url", request.getContextPath() + "/symanage/baseinfo/list.htm");
		return new ModelAndView("/massage");
	}

	/*****
	 * 关闭问卷信息
	 * 
	 * @param tbase
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "closebase.htm")
	public ModelAndView closebase(TSurveyInfoBase tbase,
			HttpServletRequest request) {
		try {
			if (tbase != null && tbase.getId() != null) {
				tbase = (TSurveyInfoBase) tSurveyInfoBaseService.queryByID(
						tbase.getId(), tbase, TSurveyInfoBase.class);
				tbase.setSy_status(SurveyConstants.SURVEY_BASE_STATUS_CLOSE);// 修改状态为关闭状态
				tSurveyInfoBaseService.updateObj(tbase);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("message", "更新失败，请重试或者联系管理员。");
		}
		request.setAttribute("message", "状态已经修改。");
		request.setAttribute("url", request.getContextPath() + "/symanage/baseinfo/list.htm");
		return new ModelAndView("/massage");
	}

	/*****
	 * 启动问卷信息
	 * 
	 * @param tbase
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "starbase.htm")
	public ModelAndView starbase(TSurveyInfoBase tbase,
			HttpServletRequest request) {
		try {
			if (tbase != null && tbase.getId() != null) {
				tbase = (TSurveyInfoBase) tSurveyInfoBaseService.queryByID(
						tbase.getId(), tbase, TSurveyInfoBase.class);
				tbase.setSy_status(SurveyConstants.SURVEY_BASE_STATUS_STAR);// 修改状态为开启状态
				tSurveyInfoBaseService.updateObj(tbase);
			}
		} catch (ServiceException e) {
			e.printStackTrace();
			request.setAttribute("message", "更新失败，请重试或者联系管理员。");
		}
		request.setAttribute("message", "状态已经修改。");
		request.setAttribute("url", request.getContextPath() + "/symanage/baseinfo/list.htm");
		return new ModelAndView("/massage");
	}

	/****
	 * 创建发布的方法
	 * 
	 * @return
	 */
	@RequestMapping(value = "publish.htm")
	public ModelAndView publish(TSurveyInfoBase tbase,
			HttpServletRequest request, HttpSession session, ModelMap map) {
		
		Integer syint = (tbase.getId() != null) ? tbase.getId() : 0;

		// 创建表对象信息：table 创建对象资源表信息
		tsurveyinfoobjtableservice.createTableBySyid(tbase.getId());
		//判断动态生成的字段处理
		tsurveyinfoobjtableservice.publishTableObjBysyid(tbase.getId());
		// 进行文件生成。
		String outfilename = tSurveyInfoBaseService.ispublish(syint);

		System.out.println("outfilename" + outfilename);
		// 进行日志记录  outfilename/front/publish/SY-1-20140323194259.jsp
		TSurveyLogPublish lopub = new TSurveyLogPublish();
		lopub.setPu_url(outfilename);
		Integer tmax = (Integer) tSurveyInfoBaseService.getMaxID(lopub) + 1;
		lopub.setId(tmax);
		lopub.setPerson((String) session
				.getAttribute(SurveyConstants.LOGIN_SESSION_LOGINNAME));
		lopub.setPu_filename(outfilename.substring(outfilename.indexOf("SY"),
				outfilename.length()));
		lopub.setPu_url(request.getContextPath() + outfilename);
		lopub.setSy_id(tbase.getId());
		lopub.setCreatetime(DateUtils.toStringAll(new Date()));
		lopub.setPu_ip(RequestGetIp.getIpAddr(request));

		try {
			// 添加发布日志信息
			tSurveyInfoBaseService.saveObj(lopub);
			
			
			//修改一发布状态
			if (tbase != null && tbase.getId() != null) {
				tbase = (TSurveyInfoBase) tSurveyInfoBaseService.queryByID(
						tbase.getId(), tbase, TSurveyInfoBase.class);
				tbase.setSy_ispub(SurveyConstants.SURVEY_ISPUB_TRUE);// 修改状态为开启状态
				tSurveyInfoBaseService.updateObj(tbase);
			}
			
			map.put("message", "您已经发布成功！");
		} catch (ServiceException e) {
			e.printStackTrace();
			map.put("message", "发布失败，请联系管理员！");
		}
		map.put("url", request.getContextPath() + "/symanage/baseinfo/list.htm");
		return new ModelAndView("/massage");
	}

	@RequestMapping(value = "pubview.htm")
	public ModelAndView pubview(TSurveyInfoBase tbase,
			HttpServletRequest request, HttpSession session, ModelMap map) {
		
		Integer syint = (tbase.getId() != null) ? tbase.getId() : 0;
		// 创建表对象信息：table 创建对象资源表信息
		//tsurveyinfoobjtableservice.createTableBySyid(tbase.getId());
		//判断动态生成的字段处理
		//tsurveyinfoobjtableservice.publishTableObjBysyid(tbase.getId());
		// 进行文件生成。????
		String outfilename = tSurveyInfoBaseService.ispubview(syint);
		System.out.println("viewfile:" + outfilename);
		request.setAttribute("message", "已经生成预览。");
		request.setAttribute("url", request.getContextPath()+ outfilename);
		return new ModelAndView("/massage");
	}
	
	
	private TSurveyInfoObjtableService publishTableObjBysyid(Integer integer) {
		// TODO Auto-generated method stub
		return null;
	}

	/****
	 * 删除问卷信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "delebase.htm")
	public ModelAndView delebase(TSurveyInfoBase tbase,
			HttpServletRequest request) {
		if (tbase != null && tbase.getId() != null) {
			try {
				tSurveyInfoBaseService.removeObj(tbase.getId(), tbase);
			} catch (ServiceException e) {
				e.printStackTrace();
				request.setAttribute("message", "删除失败，请重试或者联系管理员。");
			}
		}
		request.setAttribute("message", "问卷已经修改。");
		request.setAttribute("url", request.getContextPath()
				+ "/symanage/baseinfo/list.htm");
		return new ModelAndView("/massage");
	}

}
